//app.js
App({
  data:{  
      servser:"https://bmss.pool-data.com/wxapp"  
  },

  onLaunch: function () {
     var url = this.data.servser+'/login/wxlogin'
     var rawData = '';
    //  var signature = '';
    //  var encryptedData ='';
    //  var iv = '';
     wx.getUserInfo({
         success: function (res) {
             rawData   = res['rawData'];
            //  signature = res['signature']; 
            //  encryptedData =res['encryptedData'];
            //  iv            =res['iv'];
         }
     })
     wx.login({
        success: function(res) {
           if (res.code) {
           //发起网络请求
              wx.request({
                url: url,
                data: {
                   code      : res.code,
                   rawData   : rawData,
                   //  signature : signature,
                   //  encryptedData:encryptedData,
                   //  iv:iv 
                },
                success:function(res){
                   if(res.data['state'] == 1){ 
                      wx.setStorage({
                        key: 'session_id',
                        data: res.data['session_id']
                      });
                      
                      wx.setStorage({
                        key: 'auth_code',
                        data: res.data['auth_code']
                      }); 
                      
                   }
                }

              })
            } else {
                console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
      });

  },
  //获取用户的信息
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  globalData:{
    userInfo   : null
  }
  
})